package com.example.demo.com.java8.date;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * LocalDateTime
 * @author chengfei5
 * @date 2018/8/22
 */
public class TestSimpleDateFormat3 {
    /**
     * 1.LocalDate LocalTime LocalDateTime
     *
     */
    @Test
    public void test(){
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);

        LocalDateTime nowTime = LocalDateTime.of(2018, 8, 22, 10, 38, 33);
        System.out.println(nowTime);
        //加一年
        LocalDateTime localDateTime1 = nowTime.plusYears(1L);
        System.out.println(localDateTime1);
        LocalDateTime localDateTime4 = nowTime.plusDays(0L);
        System.out.println(localDateTime4);
        //减两年
        LocalDateTime localDateTime2 = nowTime.minusYears(2);
        System.out.println(localDateTime2);
        //都是本地时间
        System.out.println(nowTime.getYear());
        System.out.println(nowTime.getMonth());
        System.out.println(nowTime.getDayOfMonth());
        System.out.println(nowTime.getDayOfWeek());
        System.out.println(nowTime.getHour());
        System.out.println(nowTime.getMinute());
        System.out.println(nowTime.getSecond());
    }

    /**
     * 2.Instant：时间戳（以unix元年：1970年1月1日 00:00:00 到某个时间之间的秒值）
     */
    @Test
    public void test2(){
        //默认UTC时区
        Instant instant = Instant.now();
        System.out.println(instant);
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);
        //时间戳
        System.out.println(instant.toEpochMilli());
    }

    /**
     * 3.
     *  Duration:计算两个时间之间的间隔
     *  Period:计算两个日期之间的间隔
     */
    @Test
    public void test3(){
        Instant instant = Instant.now();
        try {
            Thread.sleep(1000);
        }catch (Exception e){

        }
        Instant instant1  = Instant.now();

        Duration duration = Duration.between(instant,instant1);
        //毫秒间隔
        System.out.println(duration.toMillis());
        System.out.println("-----------------------");


        LocalTime localTime = LocalTime.now();
        try {
            Thread.sleep(1000);
        }catch (Exception e){

        }
        LocalTime localTime2  = LocalTime.now();

        Duration duration2 = Duration.between(localTime,localTime2);
        //毫秒间隔
        System.out.println(duration2.toMillis());


        LocalDate localDate = LocalDate.of(2014,1,1);

        LocalDate localDate2 = LocalDate.of(2018,2,4);

        Period period = Period.between(localDate,localDate2);
        System.out.println(period);
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }
    /**
     * TemporalAdjuster:时间校正器
     * TemporalAdjusters:一个提供TemporalAdjuster实例的工具类
     */
    @Test
    public void test4(){
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);

        LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(10);
        System.out.println(localDateTime1);

        LocalDateTime with = localDateTime.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        System.out.println(with);

        //自定义 下一个工作日
        localDateTime.with(l->{
            if(l instanceof LocalDateTime){
                LocalDateTime ldt = (LocalDateTime)l;
                DayOfWeek dayOfWeek = ldt.getDayOfWeek();
                if(dayOfWeek.equals(DayOfWeek.FRIDAY)){
                    return ldt.plusDays(3);
                }
                if(dayOfWeek.equals(DayOfWeek.SATURDAY)){
                    return ldt.plusDays(2);
                }
                return ldt.plusDays(1);
            }
            return null;
        });
    }

    /**
     * DateTimeFormatter :格式化日期时间
     */
    @Test
    public void test5(){
        DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_LOCAL_DATE;
        LocalDateTime localDateTime = LocalDateTime.now().minusDays(1);
        DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyyMMdd");

        String format = localDateTime.format(isoDateTime);
        String format2 = localDateTime.format(dateTimeFormatter2);

        System.out.println(format);
        System.out.println(format2);


        System.out.println("---------");

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        String format1 = dateTimeFormatter.format(localDateTime);

        System.out.println(format1);

        LocalDateTime parse = LocalDateTime.parse(format1, dateTimeFormatter);

        System.out.println(parse);
        DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("yyyyMMdd");

        LocalDate parse2 = LocalDate.parse("20170717",dateTimeFormatter3);
        String format3 = parse2.format(isoDateTime);

        System.out.println(parse2);
        System.out.println(format3);
    }

    /**
     * ZoneDate,ZoneTime,ZoneDateTime
     */
    @Test
    public void test6(){
        //支持的时区
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
        availableZoneIds.forEach(System.out::println);

        LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Ashgabat"));
        System.out.println(now);
        //带时区的时间 跟UTC比较
        ZonedDateTime zonedDateTime = now.atZone(ZoneId.of("Asia/Tokyo"));
        System.out.println(zonedDateTime);
        ZonedDateTime zonedDateTime1 = now.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zonedDateTime1);
    }
    @Test
    public void test7(){
        Map<Integer,String> map = new HashMap<>(0);
        map.put(1,"1");
        map.put(2,"2");
        System.out.println(map);
    }

}

